package com.example.administrator.meishi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.meishi.Dao.DownJson;
import com.example.administrator.meishi.R;
import com.example.administrator.meishi.adapter.MyAdapter;
import com.example.administrator.meishi.bean.Title;
import com.example.administrator.meishi.fragment.ContentFragment;
import com.example.administrator.meishi.tools.NewsDao;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;

public class MainActivity extends AppCompatActivity {
    private int count=0;
    private List<Fragment> list;
    private List<String> marray;
    private ViewPager vp;
    private TabLayout tabLayout;
    private Toolbar tb;
    private ListView lv;
    private DrawerLayout drawer;
    private String[] daohang;
    private Button bt_login, bt_register;
    private List<Title> titles;


    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 1) {
                List<Title> mlist = (List<Title>) msg.obj;
                List<String> array = new ArrayList<>();
                for (int i = 0; i < mlist.size(); i++) {
                    //获取解析得到的id，并传给fragment
                    ContentFragment fragment = new ContentFragment();
                    Bundle bundle = new Bundle();
                    int id = mlist.get(i).getId();
                    bundle.putInt("num", id);
                    fragment.setArguments(bundle);
                    list.add(fragment);
                    array.add(mlist.get(i).getName());
                }
                MyAdapter madapter = new MyAdapter(getSupportFragmentManager(), list, array);
                tabLayout.setTabsFromPagerAdapter(madapter);
                vp.setAdapter(madapter);
                tabLayout.setupWithViewPager(vp);
            }
            return false;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //默认初始化bmob
        Bmob.initialize(this, "666dfc1537f528a3341440a9c0b81f70");
        daohang = getResources().getStringArray(R.array.array);
        marray = new ArrayList<>();
        list = new ArrayList<>();
        initView();
        initData();
        json();
        listener();
    }


    private void json() {
        //开启新线程，完成json解析，并将得到的对象传给handler进行处理
        new Thread(new Runnable() {
            @Override
            public void run() {
                titles = DownJson.getinfo();
                Message message = Message.obtain();
                message.what = 1;
                message.obj = titles;
                handler.sendMessage(message);
            }
        }).start();
    }

    private void listener() {
        //toolbar的点击事件
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!drawer.isDrawerOpen(Gravity.LEFT)) {
                    drawer.openDrawer(Gravity.LEFT);
                } else {
                    drawer.closeDrawer(Gravity.LEFT);
                }
            }
        });
        //登录
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
            }
        });
        //注册
        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, RegisterActivity.class));
            }
        });
        //查看收藏
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String string = (String) parent.getItemAtPosition(position);
                if(string.equals("收藏")){
                    startActivity(new Intent(MainActivity.this,LookCollectActivity.class));
                }else if(string.equals("清除下载")){
                    NewsDao dao = new NewsDao(MainActivity.this);
                    dao.deletetable("delete from shen");
                    Toast.makeText(MainActivity.this, "清除成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        vp = (ViewPager) findViewById(R.id.vp);
        lv = (ListView) findViewById(R.id.lv);
        tb = (Toolbar) findViewById(R.id.tb);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_register = (Button) findViewById(R.id.bt_register);
        drawer = (DrawerLayout) findViewById(R.id.drawer);
        tb.setTitle("美视");
        tb.setNavigationIcon(R.mipmap.myback);
    }
    //抽屉的listview
    private void initData() {
        for (int i = 0; i < daohang.length; i++) {
            marray.add(daohang[i]);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, marray);
        lv.setAdapter(adapter);
    }
    //双击返回退出程序
    public void onBackPressed() {
        if(count ==0){
            Toast.makeText(this,"再按一次退出",Toast.LENGTH_SHORT).show();
            count++;
        }else{
            finish();
        }
        new Thread() {
            @Override
            public void run() {
                SystemClock.sleep(3000);
                count =0;
            }
        }.start();
    }
}
